Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * max14577.c - mfd core driver for the Maxim 14577 |
| 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electrnoics |
| 5 | * Chanwoo Choi <cw00.choi@samsung.com> |
| 6 | * Krzysztof Kozlowski <k.kozlowski@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 | * 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. |
| 17 | * |
| 18 | * This driver is based on max8997.c |
| 19 | */ |
| 20 | |
Sachin Kamat | c8016d4 | 2014-02-12 14:40:11 +0530 | [diff] [blame] | 21 | #include <linux/err.h> |
Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/mfd/core.h> |
| 25 | #include <linux/mfd/max14577.h> |
| 26 | #include <linux/mfd/max14577-private.h> |
| 27 | |
| 28 | static struct mfd_cell max14577_devs[] = { |
Krzysztof Kozlowski | a0b0ea4 | 2014-01-28 13:18:28 +0100 | [diff] [blame] | 29 | { |
| 30 | .name = "max14577-muic", |
| 31 | .of_compatible = "maxim,max14577-muic", |
| 32 | }, |
Krzysztof Kozlowski | 4109680 | 2013-11-27 15:16:17 +0100 | [diff] [blame] | 33 | { |
| 34 | .name = "max14577-regulator", |
| 35 | .of_compatible = "maxim,max14577-regulator", |
| 36 | }, |
Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 37 | { .name = "max14577-charger", }, |
| 38 | }; |
| 39 | |
| 40 | static bool max14577_volatile_reg(struct device *dev, unsigned int reg) |
| 41 | { |
| 42 | switch (reg) { |
| 43 | case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3: |
| 44 | return true; |
| 45 | default: |
| 46 | break; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | static const struct regmap_config max14577_regmap_config = { |
| 52 | .reg_bits = 8, |
| 53 | .val_bits = 8, |
| 54 | .volatile_reg = max14577_volatile_reg, |
| 55 | .max_register = MAX14577_REG_END, |
| 56 | }; |
| 57 | |
| 58 | static const struct regmap_irq max14577_irqs[] = { |
| 59 | /* INT1 interrupts */ |
| 60 | { .reg_offset = 0, .mask = INT1_ADC_MASK, }, |
| 61 | { .reg_offset = 0, .mask = INT1_ADCLOW_MASK, }, |
| 62 | { .reg_offset = 0, .mask = INT1_ADCERR_MASK, }, |
| 63 | /* INT2 interrupts */ |
| 64 | { .reg_offset = 1, .mask = INT2_CHGTYP_MASK, }, |
| 65 | { .reg_offset = 1, .mask = INT2_CHGDETRUN_MASK, }, |
| 66 | { .reg_offset = 1, .mask = INT2_DCDTMR_MASK, }, |
| 67 | { .reg_offset = 1, .mask = INT2_DBCHG_MASK, }, |
| 68 | { .reg_offset = 1, .mask = INT2_VBVOLT_MASK, }, |
| 69 | /* INT3 interrupts */ |
| 70 | { .reg_offset = 2, .mask = INT3_EOC_MASK, }, |
| 71 | { .reg_offset = 2, .mask = INT3_CGMBC_MASK, }, |
| 72 | { .reg_offset = 2, .mask = INT3_OVP_MASK, }, |
| 73 | { .reg_offset = 2, .mask = INT3_MBCCHGERR_MASK, }, |
| 74 | }; |
| 75 | |
| 76 | static const struct regmap_irq_chip max14577_irq_chip = { |
| 77 | .name = "max14577", |
| 78 | .status_base = MAX14577_REG_INT1, |
| 79 | .mask_base = MAX14577_REG_INTMASK1, |
| 80 | .mask_invert = 1, |
| 81 | .num_regs = 3, |
| 82 | .irqs = max14577_irqs, |
| 83 | .num_irqs = ARRAY_SIZE(max14577_irqs), |
| 84 | }; |
| 85 | |
| 86 | static int max14577_i2c_probe(struct i2c_client *i2c, |
| 87 | const struct i2c_device_id *id) |
| 88 | { |
| 89 | struct max14577 *max14577; |
| 90 | struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev); |
| 91 | struct device_node *np = i2c->dev.of_node; |
| 92 | u8 reg_data; |
| 93 | int ret = 0; |
| 94 | |
| 95 | if (np) { |
| 96 | pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL); |
| 97 | if (!pdata) |
| 98 | return -ENOMEM; |
| 99 | i2c->dev.platform_data = pdata; |
| 100 | } |
| 101 | |
| 102 | if (!pdata) { |
Dan Carpenter | aab5dc6 | 2013-12-04 17:07:12 +0300 | [diff] [blame] | 103 | dev_err(&i2c->dev, "No platform data found.\n"); |
Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL); |
| 108 | if (!max14577) |
| 109 | return -ENOMEM; |
| 110 | |
| 111 | i2c_set_clientdata(i2c, max14577); |
| 112 | max14577->dev = &i2c->dev; |
| 113 | max14577->i2c = i2c; |
| 114 | max14577->irq = i2c->irq; |
| 115 | |
| 116 | max14577->regmap = devm_regmap_init_i2c(i2c, &max14577_regmap_config); |
| 117 | if (IS_ERR(max14577->regmap)) { |
| 118 | ret = PTR_ERR(max14577->regmap); |
| 119 | dev_err(max14577->dev, "Failed to allocate register map: %d\n", |
| 120 | ret); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID, |
| 125 | ®_data); |
| 126 | if (ret) { |
| 127 | dev_err(max14577->dev, "Device not found on this channel: %d\n", |
| 128 | ret); |
| 129 | return ret; |
| 130 | } |
| 131 | max14577->vendor_id = ((reg_data & DEVID_VENDORID_MASK) >> |
| 132 | DEVID_VENDORID_SHIFT); |
| 133 | max14577->device_id = ((reg_data & DEVID_DEVICEID_MASK) >> |
| 134 | DEVID_DEVICEID_SHIFT); |
| 135 | dev_info(max14577->dev, "Device ID: 0x%x, vendor: 0x%x\n", |
| 136 | max14577->device_id, max14577->vendor_id); |
| 137 | |
| 138 | ret = regmap_add_irq_chip(max14577->regmap, max14577->irq, |
| 139 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0, |
| 140 | &max14577_irq_chip, |
| 141 | &max14577->irq_data); |
| 142 | if (ret != 0) { |
| 143 | dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n", |
| 144 | max14577->irq, ret); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | ret = mfd_add_devices(max14577->dev, -1, max14577_devs, |
| 149 | ARRAY_SIZE(max14577_devs), NULL, 0, |
| 150 | regmap_irq_get_domain(max14577->irq_data)); |
| 151 | if (ret < 0) |
| 152 | goto err_mfd; |
| 153 | |
| 154 | device_init_wakeup(max14577->dev, 1); |
| 155 | |
| 156 | return 0; |
| 157 | |
| 158 | err_mfd: |
| 159 | regmap_del_irq_chip(max14577->irq, max14577->irq_data); |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | static int max14577_i2c_remove(struct i2c_client *i2c) |
| 165 | { |
| 166 | struct max14577 *max14577 = i2c_get_clientdata(i2c); |
| 167 | |
| 168 | mfd_remove_devices(max14577->dev); |
| 169 | regmap_del_irq_chip(max14577->irq, max14577->irq_data); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static const struct i2c_device_id max14577_i2c_id[] = { |
| 175 | { "max14577", 0 }, |
| 176 | { } |
| 177 | }; |
| 178 | MODULE_DEVICE_TABLE(i2c, max14577_i2c_id); |
| 179 | |
Geert Uytterhoeven | 3edeb1e | 2014-01-26 11:38:42 +0100 | [diff] [blame] | 180 | #ifdef CONFIG_PM_SLEEP |
Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 181 | static int max14577_suspend(struct device *dev) |
| 182 | { |
| 183 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 184 | struct max14577 *max14577 = i2c_get_clientdata(i2c); |
| 185 | |
| 186 | if (device_may_wakeup(dev)) { |
| 187 | enable_irq_wake(max14577->irq); |
| 188 | /* |
| 189 | * MUIC IRQ must be disabled during suspend if this is |
| 190 | * a wake up source because it will be handled before |
| 191 | * resuming I2C. |
| 192 | * |
| 193 | * When device is woken up from suspend (e.g. by ADC change), |
| 194 | * an interrupt occurs before resuming I2C bus controller. |
| 195 | * Interrupt handler tries to read registers but this read |
| 196 | * will fail because I2C is still suspended. |
| 197 | */ |
| 198 | disable_irq(max14577->irq); |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int max14577_resume(struct device *dev) |
| 205 | { |
| 206 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 207 | struct max14577 *max14577 = i2c_get_clientdata(i2c); |
| 208 | |
| 209 | if (device_may_wakeup(dev)) { |
| 210 | disable_irq_wake(max14577->irq); |
| 211 | enable_irq(max14577->irq); |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
Geert Uytterhoeven | 3edeb1e | 2014-01-26 11:38:42 +0100 | [diff] [blame] | 216 | #endif /* CONFIG_PM_SLEEP */ |
Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 217 | |
| 218 | static struct of_device_id max14577_dt_match[] = { |
| 219 | { .compatible = "maxim,max14577", }, |
| 220 | {}, |
| 221 | }; |
| 222 | |
| 223 | static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume); |
| 224 | |
| 225 | static struct i2c_driver max14577_i2c_driver = { |
| 226 | .driver = { |
| 227 | .name = "max14577", |
| 228 | .owner = THIS_MODULE, |
| 229 | .pm = &max14577_pm, |
Sachin Kamat | ae679c1 | 2013-12-21 15:50:16 +0530 | [diff] [blame] | 230 | .of_match_table = max14577_dt_match, |
Chanwoo Choi | 3008ddb | 2013-11-22 16:51:05 +0100 | [diff] [blame] | 231 | }, |
| 232 | .probe = max14577_i2c_probe, |
| 233 | .remove = max14577_i2c_remove, |
| 234 | .id_table = max14577_i2c_id, |
| 235 | }; |
| 236 | |
| 237 | static int __init max14577_i2c_init(void) |
| 238 | { |
| 239 | return i2c_add_driver(&max14577_i2c_driver); |
| 240 | } |
| 241 | subsys_initcall(max14577_i2c_init); |
| 242 | |
| 243 | static void __exit max14577_i2c_exit(void) |
| 244 | { |
| 245 | i2c_del_driver(&max14577_i2c_driver); |
| 246 | } |
| 247 | module_exit(max14577_i2c_exit); |
| 248 | |
| 249 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>"); |
| 250 | MODULE_DESCRIPTION("MAXIM 14577 multi-function core driver"); |
| 251 | MODULE_LICENSE("GPL"); |