Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 1 | /* |
| 2 | * max77686.c - mfd core driver for the Maxim 77686 |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * Chiwoong Byun <woong.byun@smasung.com> |
| 6 | * Jonghwa Lee <jonghwa3.lee@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 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * This driver is based on max8997.c |
| 23 | */ |
| 24 | |
| 25 | #include <linux/export.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/i2c.h> |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 28 | #include <linux/irq.h> |
| 29 | #include <linux/interrupt.h> |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
| 31 | #include <linux/module.h> |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 32 | #include <linux/mfd/core.h> |
| 33 | #include <linux/mfd/max77686.h> |
| 34 | #include <linux/mfd/max77686-private.h> |
| 35 | #include <linux/err.h> |
Sachin Kamat | 2a048d3 | 2013-10-16 14:26:49 +0530 | [diff] [blame] | 36 | #include <linux/of.h> |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 37 | |
| 38 | #define I2C_ADDR_RTC (0x0C >> 1) |
| 39 | |
Geert Uytterhoeven | 7c0517b | 2013-11-18 14:33:00 +0100 | [diff] [blame] | 40 | static const struct mfd_cell max77686_devs[] = { |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 41 | { .name = "max77686-pmic", }, |
| 42 | { .name = "max77686-rtc", }, |
Olof Johansson | c0fa7e1 | 2013-05-10 10:11:59 -0700 | [diff] [blame] | 43 | { .name = "max77686-clk", }, |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static struct regmap_config max77686_regmap_config = { |
| 47 | .reg_bits = 8, |
| 48 | .val_bits = 8, |
| 49 | }; |
| 50 | |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 51 | static struct regmap_config max77686_rtc_regmap_config = { |
| 52 | .reg_bits = 8, |
| 53 | .val_bits = 8, |
| 54 | }; |
| 55 | |
| 56 | static const struct regmap_irq max77686_irqs[] = { |
| 57 | /* INT1 interrupts */ |
| 58 | { .reg_offset = 0, .mask = MAX77686_INT1_PWRONF_MSK, }, |
| 59 | { .reg_offset = 0, .mask = MAX77686_INT1_PWRONR_MSK, }, |
| 60 | { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBF_MSK, }, |
| 61 | { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBR_MSK, }, |
| 62 | { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBF_MSK, }, |
| 63 | { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBR_MSK, }, |
| 64 | { .reg_offset = 0, .mask = MAX77686_INT1_ONKEY1S_MSK, }, |
| 65 | { .reg_offset = 0, .mask = MAX77686_INT1_MRSTB_MSK, }, |
| 66 | /* INT2 interrupts */ |
| 67 | { .reg_offset = 1, .mask = MAX77686_INT2_140C_MSK, }, |
| 68 | { .reg_offset = 1, .mask = MAX77686_INT2_120C_MSK, }, |
| 69 | }; |
| 70 | |
| 71 | static const struct regmap_irq_chip max77686_irq_chip = { |
| 72 | .name = "max77686-pmic", |
| 73 | .status_base = MAX77686_REG_INT1, |
| 74 | .mask_base = MAX77686_REG_INT1MSK, |
| 75 | .num_regs = 2, |
| 76 | .irqs = max77686_irqs, |
| 77 | .num_irqs = ARRAY_SIZE(max77686_irqs), |
| 78 | }; |
| 79 | |
| 80 | static const struct regmap_irq max77686_rtc_irqs[] = { |
| 81 | /* RTC interrupts */ |
| 82 | { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC60S_MSK, }, |
| 83 | { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA1_MSK, }, |
| 84 | { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA2_MSK, }, |
| 85 | { .reg_offset = 0, .mask = MAX77686_RTCINT_SMPL_MSK, }, |
| 86 | { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC1S_MSK, }, |
| 87 | { .reg_offset = 0, .mask = MAX77686_RTCINT_WTSR_MSK, }, |
| 88 | }; |
| 89 | |
| 90 | static const struct regmap_irq_chip max77686_rtc_irq_chip = { |
| 91 | .name = "max77686-rtc", |
| 92 | .status_base = MAX77686_RTC_INT, |
| 93 | .mask_base = MAX77686_RTC_INTM, |
| 94 | .num_regs = 1, |
| 95 | .irqs = max77686_rtc_irqs, |
| 96 | .num_irqs = ARRAY_SIZE(max77686_rtc_irqs), |
| 97 | }; |
| 98 | |
Krzysztof Kozlowski | b68ece3 | 2014-05-13 12:58:51 +0200 | [diff] [blame] | 99 | static const struct of_device_id max77686_pmic_dt_match[] = { |
Sachin Kamat | d1ac2c0 | 2013-03-26 10:13:36 +0530 | [diff] [blame] | 100 | {.compatible = "maxim,max77686", .data = NULL}, |
Axel Lin | 2984fc0 | 2012-07-09 22:29:00 +0800 | [diff] [blame] | 101 | {}, |
| 102 | }; |
| 103 | |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 104 | static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device |
| 105 | *dev) |
| 106 | { |
| 107 | struct max77686_platform_data *pd; |
| 108 | |
| 109 | pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); |
Javier Martinez Canillas | 8a789b6 | 2014-07-04 22:24:10 +0200 | [diff] [blame^] | 110 | if (!pd) |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 111 | return NULL; |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 112 | |
| 113 | dev->platform_data = pd; |
| 114 | return pd; |
| 115 | } |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 116 | |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 117 | static int max77686_i2c_probe(struct i2c_client *i2c, |
| 118 | const struct i2c_device_id *id) |
| 119 | { |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 120 | struct max77686_dev *max77686 = NULL; |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 121 | struct max77686_platform_data *pdata = dev_get_platdata(&i2c->dev); |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 122 | unsigned int data; |
| 123 | int ret = 0; |
| 124 | |
Javier Martinez Canillas | ede04c6 | 2014-07-04 22:24:07 +0200 | [diff] [blame] | 125 | if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node && !pdata) |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 126 | pdata = max77686_i2c_parse_dt_pdata(&i2c->dev); |
| 127 | |
| 128 | if (!pdata) { |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 129 | dev_err(&i2c->dev, "No platform data found.\n"); |
Javier Martinez Canillas | b452d7b | 2014-07-04 22:24:08 +0200 | [diff] [blame] | 130 | return -EINVAL; |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 131 | } |
| 132 | |
Lee Jones | 742413a | 2013-05-23 16:25:16 +0100 | [diff] [blame] | 133 | max77686 = devm_kzalloc(&i2c->dev, |
| 134 | sizeof(struct max77686_dev), GFP_KERNEL); |
Javier Martinez Canillas | c0e0fcd | 2014-07-04 22:24:09 +0200 | [diff] [blame] | 135 | if (!max77686) |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 136 | return -ENOMEM; |
| 137 | |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 138 | i2c_set_clientdata(i2c, max77686); |
| 139 | max77686->dev = &i2c->dev; |
| 140 | max77686->i2c = i2c; |
| 141 | max77686->type = id->driver_data; |
| 142 | |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 143 | max77686->wakeup = pdata->wakeup; |
Yadwinder Singh Brar | 2b40459 | 2012-07-09 13:21:45 +0200 | [diff] [blame] | 144 | max77686->irq = i2c->irq; |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 145 | |
Krzysztof Kozlowski | 74142ff | 2013-12-20 10:35:07 +0100 | [diff] [blame] | 146 | max77686->regmap = devm_regmap_init_i2c(i2c, &max77686_regmap_config); |
Axel Lin | 136d982 | 2012-12-25 00:16:43 +0800 | [diff] [blame] | 147 | if (IS_ERR(max77686->regmap)) { |
| 148 | ret = PTR_ERR(max77686->regmap); |
| 149 | dev_err(max77686->dev, "Failed to allocate register map: %d\n", |
| 150 | ret); |
Axel Lin | 136d982 | 2012-12-25 00:16:43 +0800 | [diff] [blame] | 151 | return ret; |
| 152 | } |
| 153 | |
Javier Martinez Canillas | c0e0fcd | 2014-07-04 22:24:09 +0200 | [diff] [blame] | 154 | ret = regmap_read(max77686->regmap, MAX77686_REG_DEVICE_ID, &data); |
| 155 | if (ret < 0) { |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 156 | dev_err(max77686->dev, |
| 157 | "device not found on this channel (this is not an error)\n"); |
Lee Jones | 742413a | 2013-05-23 16:25:16 +0100 | [diff] [blame] | 158 | return -ENODEV; |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 159 | } |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 160 | |
| 161 | max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC); |
Krzysztof Kozlowski | b9e183a | 2014-02-11 11:03:31 +0100 | [diff] [blame] | 162 | if (!max77686->rtc) { |
| 163 | dev_err(max77686->dev, "Failed to allocate I2C device for RTC\n"); |
| 164 | return -ENODEV; |
| 165 | } |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 166 | i2c_set_clientdata(max77686->rtc, max77686); |
| 167 | |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 168 | max77686->rtc_regmap = devm_regmap_init_i2c(max77686->rtc, |
| 169 | &max77686_rtc_regmap_config); |
| 170 | if (IS_ERR(max77686->rtc_regmap)) { |
| 171 | ret = PTR_ERR(max77686->rtc_regmap); |
| 172 | dev_err(max77686->dev, "failed to allocate RTC regmap: %d\n", |
| 173 | ret); |
| 174 | goto err_unregister_i2c; |
| 175 | } |
| 176 | |
| 177 | ret = regmap_add_irq_chip(max77686->regmap, max77686->irq, |
| 178 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT | |
| 179 | IRQF_SHARED, 0, &max77686_irq_chip, |
| 180 | &max77686->irq_data); |
Javier Martinez Canillas | c0e0fcd | 2014-07-04 22:24:09 +0200 | [diff] [blame] | 181 | if (ret) { |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 182 | dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret); |
| 183 | goto err_unregister_i2c; |
| 184 | } |
| 185 | ret = regmap_add_irq_chip(max77686->rtc_regmap, max77686->irq, |
| 186 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT | |
| 187 | IRQF_SHARED, 0, &max77686_rtc_irq_chip, |
| 188 | &max77686->rtc_irq_data); |
Javier Martinez Canillas | c0e0fcd | 2014-07-04 22:24:09 +0200 | [diff] [blame] | 189 | if (ret) { |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 190 | dev_err(&i2c->dev, "failed to add RTC irq chip: %d\n", ret); |
| 191 | goto err_del_irqc; |
| 192 | } |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 193 | |
| 194 | ret = mfd_add_devices(max77686->dev, -1, max77686_devs, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 195 | ARRAY_SIZE(max77686_devs), NULL, 0, NULL); |
Lee Jones | 742413a | 2013-05-23 16:25:16 +0100 | [diff] [blame] | 196 | if (ret < 0) { |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 197 | dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret); |
| 198 | goto err_del_rtc_irqc; |
Lee Jones | 742413a | 2013-05-23 16:25:16 +0100 | [diff] [blame] | 199 | } |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 200 | |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 201 | return 0; |
| 202 | |
| 203 | err_del_rtc_irqc: |
| 204 | regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data); |
| 205 | err_del_irqc: |
| 206 | regmap_del_irq_chip(max77686->irq, max77686->irq_data); |
| 207 | err_unregister_i2c: |
| 208 | i2c_unregister_device(max77686->rtc); |
| 209 | |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static int max77686_i2c_remove(struct i2c_client *i2c) |
| 214 | { |
| 215 | struct max77686_dev *max77686 = i2c_get_clientdata(i2c); |
| 216 | |
| 217 | mfd_remove_devices(max77686->dev); |
Javier Martinez Canillas | 6f1c1e7 | 2014-07-04 22:24:04 +0200 | [diff] [blame] | 218 | |
| 219 | regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data); |
| 220 | regmap_del_irq_chip(max77686->irq, max77686->irq_data); |
| 221 | |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 222 | i2c_unregister_device(max77686->rtc); |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static const struct i2c_device_id max77686_i2c_id[] = { |
| 228 | { "max77686", TYPE_MAX77686 }, |
| 229 | { } |
| 230 | }; |
| 231 | MODULE_DEVICE_TABLE(i2c, max77686_i2c_id); |
| 232 | |
Javier Martinez Canillas | 2b52b5d | 2014-07-04 22:24:05 +0200 | [diff] [blame] | 233 | #ifdef CONFIG_PM_SLEEP |
| 234 | static int max77686_suspend(struct device *dev) |
| 235 | { |
| 236 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 237 | struct max77686_dev *max77686 = i2c_get_clientdata(i2c); |
| 238 | |
| 239 | if (device_may_wakeup(dev)) |
| 240 | enable_irq_wake(max77686->irq); |
| 241 | |
| 242 | /* |
| 243 | * IRQ must be disabled during suspend because if it happens |
| 244 | * while suspended it will be handled before resuming I2C. |
| 245 | * |
| 246 | * When device is woken up from suspend (e.g. by RTC wake alarm), |
| 247 | * an interrupt occurs before resuming I2C bus controller. |
| 248 | * Interrupt handler tries to read registers but this read |
| 249 | * will fail because I2C is still suspended. |
| 250 | */ |
| 251 | disable_irq(max77686->irq); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int max77686_resume(struct device *dev) |
| 257 | { |
| 258 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 259 | struct max77686_dev *max77686 = i2c_get_clientdata(i2c); |
| 260 | |
| 261 | if (device_may_wakeup(dev)) |
| 262 | disable_irq_wake(max77686->irq); |
| 263 | |
| 264 | enable_irq(max77686->irq); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | #endif /* CONFIG_PM_SLEEP */ |
| 269 | |
| 270 | static SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume); |
| 271 | |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 272 | static struct i2c_driver max77686_i2c_driver = { |
| 273 | .driver = { |
| 274 | .name = "max77686", |
| 275 | .owner = THIS_MODULE, |
Javier Martinez Canillas | 2b52b5d | 2014-07-04 22:24:05 +0200 | [diff] [blame] | 276 | .pm = &max77686_pm, |
Yadwinder Singh Brar | c1516f8 | 2012-07-06 17:02:55 +0530 | [diff] [blame] | 277 | .of_match_table = of_match_ptr(max77686_pmic_dt_match), |
Jonghwa Lee | dae8a96 | 2012-06-25 10:34:36 +0200 | [diff] [blame] | 278 | }, |
| 279 | .probe = max77686_i2c_probe, |
| 280 | .remove = max77686_i2c_remove, |
| 281 | .id_table = max77686_i2c_id, |
| 282 | }; |
| 283 | |
| 284 | static int __init max77686_i2c_init(void) |
| 285 | { |
| 286 | return i2c_add_driver(&max77686_i2c_driver); |
| 287 | } |
| 288 | /* init early so consumer devices can complete system boot */ |
| 289 | subsys_initcall(max77686_i2c_init); |
| 290 | |
| 291 | static void __exit max77686_i2c_exit(void) |
| 292 | { |
| 293 | i2c_del_driver(&max77686_i2c_driver); |
| 294 | } |
| 295 | module_exit(max77686_i2c_exit); |
| 296 | |
| 297 | MODULE_DESCRIPTION("MAXIM 77686 multi-function core driver"); |
| 298 | MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>"); |
| 299 | MODULE_LICENSE("GPL"); |