David Collins | 8885f79 | 2017-01-26 14:36:34 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2016-2017 The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "I2C PMIC: %s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/irq.h> |
| 19 | #include <linux/irqdomain.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/pinctrl/consumer.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/slab.h> |
| 25 | |
| 26 | #define I2C_INTR_STATUS_BASE 0x0550 |
| 27 | #define INT_RT_STS_OFFSET 0x10 |
| 28 | #define INT_SET_TYPE_OFFSET 0x11 |
| 29 | #define INT_POL_HIGH_OFFSET 0x12 |
| 30 | #define INT_POL_LOW_OFFSET 0x13 |
| 31 | #define INT_LATCHED_CLR_OFFSET 0x14 |
| 32 | #define INT_EN_SET_OFFSET 0x15 |
| 33 | #define INT_EN_CLR_OFFSET 0x16 |
| 34 | #define INT_LATCHED_STS_OFFSET 0x18 |
| 35 | #define INT_PENDING_STS_OFFSET 0x19 |
| 36 | #define INT_MID_SEL_OFFSET 0x1A |
| 37 | #define INT_MID_SEL_MASK GENMASK(1, 0) |
| 38 | #define INT_PRIORITY_OFFSET 0x1B |
| 39 | #define INT_PRIORITY_BIT BIT(0) |
| 40 | |
| 41 | enum { |
| 42 | IRQ_SET_TYPE = 0, |
| 43 | IRQ_POL_HIGH, |
| 44 | IRQ_POL_LOW, |
| 45 | IRQ_LATCHED_CLR, /* not needed but makes life easy */ |
| 46 | IRQ_EN_SET, |
| 47 | IRQ_MAX_REGS, |
| 48 | }; |
| 49 | |
| 50 | struct i2c_pmic_periph { |
| 51 | void *data; |
| 52 | u16 addr; |
| 53 | u8 cached[IRQ_MAX_REGS]; |
| 54 | u8 synced[IRQ_MAX_REGS]; |
| 55 | u8 wake; |
| 56 | struct mutex lock; |
| 57 | }; |
| 58 | |
| 59 | struct i2c_pmic { |
| 60 | struct device *dev; |
| 61 | struct regmap *regmap; |
| 62 | struct irq_domain *domain; |
| 63 | struct i2c_pmic_periph *periph; |
| 64 | struct pinctrl *pinctrl; |
| 65 | const char *pinctrl_name; |
| 66 | int num_periphs; |
| 67 | }; |
| 68 | |
| 69 | static void i2c_pmic_irq_bus_lock(struct irq_data *d) |
| 70 | { |
| 71 | struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d); |
| 72 | |
| 73 | mutex_lock(&periph->lock); |
| 74 | } |
| 75 | |
| 76 | static void i2c_pmic_sync_type_polarity(struct i2c_pmic *chip, |
| 77 | struct i2c_pmic_periph *periph) |
| 78 | { |
| 79 | int rc; |
| 80 | |
| 81 | /* did any irq type change? */ |
| 82 | if (periph->cached[IRQ_SET_TYPE] ^ periph->synced[IRQ_SET_TYPE]) { |
| 83 | rc = regmap_write(chip->regmap, |
| 84 | periph->addr | INT_SET_TYPE_OFFSET, |
| 85 | periph->cached[IRQ_SET_TYPE]); |
| 86 | if (rc < 0) { |
| 87 | pr_err("Couldn't set periph 0x%04x irqs 0x%02x type rc=%d\n", |
| 88 | periph->addr, periph->cached[IRQ_SET_TYPE], rc); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | periph->synced[IRQ_SET_TYPE] = periph->cached[IRQ_SET_TYPE]; |
| 93 | } |
| 94 | |
| 95 | /* did any polarity high change? */ |
| 96 | if (periph->cached[IRQ_POL_HIGH] ^ periph->synced[IRQ_POL_HIGH]) { |
| 97 | rc = regmap_write(chip->regmap, |
| 98 | periph->addr | INT_POL_HIGH_OFFSET, |
| 99 | periph->cached[IRQ_POL_HIGH]); |
| 100 | if (rc < 0) { |
| 101 | pr_err("Couldn't set periph 0x%04x irqs 0x%02x polarity high rc=%d\n", |
| 102 | periph->addr, periph->cached[IRQ_POL_HIGH], rc); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | periph->synced[IRQ_POL_HIGH] = periph->cached[IRQ_POL_HIGH]; |
| 107 | } |
| 108 | |
| 109 | /* did any polarity low change? */ |
| 110 | if (periph->cached[IRQ_POL_LOW] ^ periph->synced[IRQ_POL_LOW]) { |
| 111 | rc = regmap_write(chip->regmap, |
| 112 | periph->addr | INT_POL_LOW_OFFSET, |
| 113 | periph->cached[IRQ_POL_LOW]); |
| 114 | if (rc < 0) { |
| 115 | pr_err("Couldn't set periph 0x%04x irqs 0x%02x polarity low rc=%d\n", |
| 116 | periph->addr, periph->cached[IRQ_POL_LOW], rc); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | periph->synced[IRQ_POL_LOW] = periph->cached[IRQ_POL_LOW]; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void i2c_pmic_sync_enable(struct i2c_pmic *chip, |
| 125 | struct i2c_pmic_periph *periph) |
| 126 | { |
| 127 | u8 en_set, en_clr; |
| 128 | int rc; |
| 129 | |
| 130 | /* determine which irqs were enabled and which were disabled */ |
| 131 | en_clr = periph->synced[IRQ_EN_SET] & ~periph->cached[IRQ_EN_SET]; |
| 132 | en_set = ~periph->synced[IRQ_EN_SET] & periph->cached[IRQ_EN_SET]; |
| 133 | |
| 134 | /* were any irqs disabled? */ |
| 135 | if (en_clr) { |
| 136 | rc = regmap_write(chip->regmap, |
| 137 | periph->addr | INT_EN_CLR_OFFSET, en_clr); |
| 138 | if (rc < 0) { |
| 139 | pr_err("Couldn't disable periph 0x%04x irqs 0x%02x rc=%d\n", |
| 140 | periph->addr, en_clr, rc); |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* were any irqs enabled? */ |
| 146 | if (en_set) { |
| 147 | rc = regmap_write(chip->regmap, |
| 148 | periph->addr | INT_EN_SET_OFFSET, en_set); |
| 149 | if (rc < 0) { |
| 150 | pr_err("Couldn't enable periph 0x%04x irqs 0x%02x rc=%d\n", |
| 151 | periph->addr, en_set, rc); |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* irq enabled status was written to hardware */ |
| 157 | periph->synced[IRQ_EN_SET] = periph->cached[IRQ_EN_SET]; |
| 158 | } |
| 159 | |
| 160 | static void i2c_pmic_irq_bus_sync_unlock(struct irq_data *d) |
| 161 | { |
| 162 | struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d); |
| 163 | struct i2c_pmic *chip = periph->data; |
| 164 | |
| 165 | i2c_pmic_sync_type_polarity(chip, periph); |
| 166 | i2c_pmic_sync_enable(chip, periph); |
| 167 | mutex_unlock(&periph->lock); |
| 168 | } |
| 169 | |
| 170 | static void i2c_pmic_irq_disable(struct irq_data *d) |
| 171 | { |
| 172 | struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d); |
| 173 | |
| 174 | periph->cached[IRQ_EN_SET] &= ~d->hwirq & 0xFF; |
| 175 | } |
| 176 | |
| 177 | static void i2c_pmic_irq_enable(struct irq_data *d) |
| 178 | { |
| 179 | struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d); |
| 180 | |
| 181 | periph->cached[IRQ_EN_SET] |= d->hwirq & 0xFF; |
| 182 | } |
| 183 | |
| 184 | static int i2c_pmic_irq_set_type(struct irq_data *d, unsigned int irq_type) |
| 185 | { |
| 186 | struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d); |
| 187 | |
| 188 | switch (irq_type) { |
| 189 | case IRQ_TYPE_EDGE_RISING: |
| 190 | periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF; |
| 191 | periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF; |
| 192 | periph->cached[IRQ_POL_LOW] &= ~d->hwirq & 0xFF; |
| 193 | break; |
| 194 | case IRQ_TYPE_EDGE_FALLING: |
| 195 | periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF; |
| 196 | periph->cached[IRQ_POL_HIGH] &= ~d->hwirq & 0xFF; |
| 197 | periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF; |
| 198 | break; |
| 199 | case IRQ_TYPE_EDGE_BOTH: |
| 200 | periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF; |
| 201 | periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF; |
| 202 | periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF; |
| 203 | break; |
| 204 | case IRQ_TYPE_LEVEL_HIGH: |
| 205 | periph->cached[IRQ_SET_TYPE] &= ~d->hwirq & 0xFF; |
| 206 | periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF; |
| 207 | periph->cached[IRQ_POL_LOW] &= ~d->hwirq & 0xFF; |
| 208 | break; |
| 209 | case IRQ_TYPE_LEVEL_LOW: |
| 210 | periph->cached[IRQ_SET_TYPE] &= ~d->hwirq & 0xFF; |
| 211 | periph->cached[IRQ_POL_HIGH] &= ~d->hwirq & 0xFF; |
| 212 | periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF; |
| 213 | break; |
| 214 | default: |
| 215 | pr_err("irq type 0x%04x is not supported\n", irq_type); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | #ifdef CONFIG_PM_SLEEP |
| 223 | static int i2c_pmic_irq_set_wake(struct irq_data *d, unsigned int on) |
| 224 | { |
| 225 | struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d); |
| 226 | |
| 227 | if (on) |
| 228 | periph->wake |= d->hwirq & 0xFF; |
| 229 | else |
| 230 | periph->wake &= ~d->hwirq & 0xFF; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | #else |
| 235 | #define i2c_pmic_irq_set_wake NULL |
| 236 | #endif |
| 237 | |
| 238 | static struct irq_chip i2c_pmic_irq_chip = { |
| 239 | .name = "i2c_pmic_irq_chip", |
| 240 | .irq_bus_lock = i2c_pmic_irq_bus_lock, |
| 241 | .irq_bus_sync_unlock = i2c_pmic_irq_bus_sync_unlock, |
| 242 | .irq_disable = i2c_pmic_irq_disable, |
| 243 | .irq_enable = i2c_pmic_irq_enable, |
| 244 | .irq_set_type = i2c_pmic_irq_set_type, |
| 245 | .irq_set_wake = i2c_pmic_irq_set_wake, |
| 246 | }; |
| 247 | |
| 248 | static struct i2c_pmic_periph *i2c_pmic_find_periph(struct i2c_pmic *chip, |
| 249 | irq_hw_number_t hwirq) |
| 250 | { |
| 251 | int i; |
| 252 | |
| 253 | for (i = 0; i < chip->num_periphs; i++) |
| 254 | if (chip->periph[i].addr == (hwirq & 0xFF00)) |
| 255 | return &chip->periph[i]; |
| 256 | |
| 257 | pr_err_ratelimited("Couldn't find periph struct for hwirq 0x%04lx\n", |
| 258 | hwirq); |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | static int i2c_pmic_domain_map(struct irq_domain *d, unsigned int virq, |
| 263 | irq_hw_number_t hwirq) |
| 264 | { |
| 265 | struct i2c_pmic *chip = d->host_data; |
| 266 | struct i2c_pmic_periph *periph = i2c_pmic_find_periph(chip, hwirq); |
| 267 | |
| 268 | if (!periph) |
| 269 | return -ENODEV; |
| 270 | |
| 271 | irq_set_chip_data(virq, periph); |
| 272 | irq_set_chip_and_handler(virq, &i2c_pmic_irq_chip, handle_level_irq); |
| 273 | irq_set_nested_thread(virq, 1); |
| 274 | irq_set_noprobe(virq); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int i2c_pmic_domain_xlate(struct irq_domain *d, |
| 279 | struct device_node *ctrlr, const u32 *intspec, |
| 280 | unsigned int intsize, unsigned long *out_hwirq, |
| 281 | unsigned int *out_type) |
| 282 | { |
| 283 | if (intsize != 3) |
| 284 | return -EINVAL; |
| 285 | |
| 286 | if (intspec[0] > 0xFF || intspec[1] > 0x7 || |
| 287 | intspec[2] > IRQ_TYPE_SENSE_MASK) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | /* |
| 291 | * Interrupt specifiers are triplets |
| 292 | * <peripheral-address, irq-number, IRQ_TYPE_*> |
| 293 | * |
| 294 | * peripheral-address - The base address of the peripheral |
| 295 | * irq-number - The zero based bit position of the peripheral's |
| 296 | * interrupt registers corresponding to the irq |
| 297 | * where the LSB is 0 and the MSB is 7 |
| 298 | * IRQ_TYPE_* - Please refer to linux/irq.h |
| 299 | */ |
| 300 | *out_hwirq = intspec[0] << 8 | BIT(intspec[1]); |
| 301 | *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static const struct irq_domain_ops i2c_pmic_domain_ops = { |
| 307 | .map = i2c_pmic_domain_map, |
| 308 | .xlate = i2c_pmic_domain_xlate, |
| 309 | }; |
| 310 | |
| 311 | static void i2c_pmic_irq_ack_now(struct i2c_pmic *chip, u16 hwirq) |
| 312 | { |
| 313 | int rc; |
| 314 | |
| 315 | rc = regmap_write(chip->regmap, |
| 316 | (hwirq & 0xFF00) | INT_LATCHED_CLR_OFFSET, |
| 317 | hwirq & 0xFF); |
| 318 | if (rc < 0) |
| 319 | pr_err_ratelimited("Couldn't ack 0x%04x rc=%d\n", hwirq, rc); |
| 320 | } |
| 321 | |
| 322 | static void i2c_pmic_irq_disable_now(struct i2c_pmic *chip, u16 hwirq) |
| 323 | { |
| 324 | struct i2c_pmic_periph *periph = i2c_pmic_find_periph(chip, hwirq); |
| 325 | int rc; |
| 326 | |
| 327 | if (!periph) |
| 328 | return; |
| 329 | |
| 330 | mutex_lock(&periph->lock); |
| 331 | periph->cached[IRQ_EN_SET] &= ~hwirq & 0xFF; |
| 332 | |
| 333 | rc = regmap_write(chip->regmap, |
| 334 | (hwirq & 0xFF00) | INT_EN_CLR_OFFSET, |
| 335 | hwirq & 0xFF); |
| 336 | if (rc < 0) { |
| 337 | pr_err_ratelimited("Couldn't disable irq 0x%04x rc=%d\n", |
| 338 | hwirq, rc); |
| 339 | goto unlock; |
| 340 | } |
| 341 | |
| 342 | periph->synced[IRQ_EN_SET] = periph->cached[IRQ_EN_SET]; |
| 343 | |
| 344 | unlock: |
| 345 | mutex_unlock(&periph->lock); |
| 346 | } |
| 347 | |
| 348 | static void i2c_pmic_periph_status_handler(struct i2c_pmic *chip, |
| 349 | u16 periph_address, u8 periph_status) |
| 350 | { |
| 351 | unsigned int hwirq, virq; |
| 352 | int i; |
| 353 | |
| 354 | while (periph_status) { |
| 355 | i = ffs(periph_status) - 1; |
| 356 | periph_status &= ~BIT(i); |
| 357 | hwirq = periph_address | BIT(i); |
| 358 | virq = irq_find_mapping(chip->domain, hwirq); |
| 359 | if (virq == 0) { |
| 360 | pr_err_ratelimited("Couldn't find mapping; disabling 0x%04x\n", |
| 361 | hwirq); |
| 362 | i2c_pmic_irq_disable_now(chip, hwirq); |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | handle_nested_irq(virq); |
| 367 | i2c_pmic_irq_ack_now(chip, hwirq); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | static void i2c_pmic_summary_status_handler(struct i2c_pmic *chip, |
| 372 | struct i2c_pmic_periph *periph, |
| 373 | u8 summary_status) |
| 374 | { |
| 375 | unsigned int periph_status; |
| 376 | int rc, i; |
| 377 | |
| 378 | while (summary_status) { |
| 379 | i = ffs(summary_status) - 1; |
| 380 | summary_status &= ~BIT(i); |
| 381 | |
| 382 | rc = regmap_read(chip->regmap, |
| 383 | periph[i].addr | INT_LATCHED_STS_OFFSET, |
| 384 | &periph_status); |
| 385 | if (rc < 0) { |
| 386 | pr_err_ratelimited("Couldn't read 0x%04x | INT_LATCHED_STS rc=%d\n", |
| 387 | periph[i].addr, rc); |
| 388 | continue; |
| 389 | } |
| 390 | |
| 391 | i2c_pmic_periph_status_handler(chip, periph[i].addr, |
| 392 | periph_status); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static irqreturn_t i2c_pmic_irq_handler(int irq, void *dev_id) |
| 397 | { |
| 398 | struct i2c_pmic *chip = dev_id; |
| 399 | struct i2c_pmic_periph *periph; |
| 400 | unsigned int summary_status; |
| 401 | int rc, i; |
| 402 | |
| 403 | for (i = 0; i < DIV_ROUND_UP(chip->num_periphs, BITS_PER_BYTE); i++) { |
| 404 | rc = regmap_read(chip->regmap, I2C_INTR_STATUS_BASE + i, |
| 405 | &summary_status); |
| 406 | if (rc < 0) { |
| 407 | pr_err_ratelimited("Couldn't read I2C_INTR_STATUS%d rc=%d\n", |
| 408 | i, rc); |
| 409 | continue; |
| 410 | } |
| 411 | |
| 412 | if (summary_status == 0) |
| 413 | continue; |
| 414 | |
| 415 | periph = &chip->periph[i * 8]; |
| 416 | i2c_pmic_summary_status_handler(chip, periph, summary_status); |
| 417 | } |
| 418 | |
| 419 | return IRQ_HANDLED; |
| 420 | } |
| 421 | |
| 422 | static int i2c_pmic_parse_dt(struct i2c_pmic *chip) |
| 423 | { |
| 424 | struct device_node *node = chip->dev->of_node; |
| 425 | int rc, i; |
| 426 | u32 temp; |
| 427 | |
| 428 | if (!node) { |
| 429 | pr_err("missing device tree\n"); |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | |
| 433 | chip->num_periphs = of_property_count_u32_elems(node, |
| 434 | "qcom,periph-map"); |
| 435 | if (chip->num_periphs < 0) { |
| 436 | pr_err("missing qcom,periph-map property rc=%d\n", |
| 437 | chip->num_periphs); |
| 438 | return chip->num_periphs; |
| 439 | } |
| 440 | |
| 441 | if (chip->num_periphs == 0) { |
| 442 | pr_err("qcom,periph-map must contain at least one address\n"); |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
| 446 | chip->periph = devm_kcalloc(chip->dev, chip->num_periphs, |
| 447 | sizeof(*chip->periph), GFP_KERNEL); |
| 448 | if (!chip->periph) |
| 449 | return -ENOMEM; |
| 450 | |
| 451 | for (i = 0; i < chip->num_periphs; i++) { |
| 452 | rc = of_property_read_u32_index(node, "qcom,periph-map", |
| 453 | i, &temp); |
| 454 | if (rc < 0) { |
| 455 | pr_err("Couldn't read qcom,periph-map[%d] rc=%d\n", |
| 456 | i, rc); |
| 457 | return rc; |
| 458 | } |
| 459 | |
| 460 | chip->periph[i].addr = (u16)(temp << 8); |
| 461 | chip->periph[i].data = chip; |
| 462 | mutex_init(&chip->periph[i].lock); |
| 463 | } |
| 464 | |
| 465 | of_property_read_string(node, "pinctrl-names", &chip->pinctrl_name); |
| 466 | |
| 467 | return rc; |
| 468 | } |
| 469 | |
| 470 | #define MAX_I2C_RETRIES 3 |
| 471 | static int i2c_pmic_read(struct regmap *map, unsigned int reg, void *val, |
| 472 | size_t val_count) |
| 473 | { |
| 474 | int rc, retries = 0; |
| 475 | |
| 476 | do { |
| 477 | rc = regmap_bulk_read(map, reg, val, val_count); |
| 478 | } while (rc == -ENOTCONN && retries++ < MAX_I2C_RETRIES); |
| 479 | |
| 480 | if (retries > 1) |
| 481 | pr_err("i2c_pmic_read failed for %d retries, rc = %d\n", |
| 482 | retries - 1, rc); |
| 483 | |
| 484 | return rc; |
| 485 | } |
| 486 | |
| 487 | static int i2c_pmic_determine_initial_status(struct i2c_pmic *chip) |
| 488 | { |
| 489 | int rc, i; |
| 490 | |
| 491 | for (i = 0; i < chip->num_periphs; i++) { |
| 492 | rc = i2c_pmic_read(chip->regmap, |
| 493 | chip->periph[i].addr | INT_SET_TYPE_OFFSET, |
| 494 | chip->periph[i].cached, IRQ_MAX_REGS); |
| 495 | if (rc < 0) { |
| 496 | pr_err("Couldn't read irq data rc=%d\n", rc); |
| 497 | return rc; |
| 498 | } |
| 499 | |
| 500 | memcpy(chip->periph[i].synced, chip->periph[i].cached, |
| 501 | IRQ_MAX_REGS * sizeof(*chip->periph[i].synced)); |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static struct regmap_config i2c_pmic_regmap_config = { |
| 508 | .reg_bits = 16, |
| 509 | .val_bits = 8, |
| 510 | .max_register = 0xFFFF, |
| 511 | }; |
| 512 | |
| 513 | static int i2c_pmic_probe(struct i2c_client *client, |
| 514 | const struct i2c_device_id *id) |
| 515 | { |
| 516 | struct i2c_pmic *chip; |
| 517 | int rc = 0; |
| 518 | |
| 519 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 520 | if (!chip) |
| 521 | return -ENOMEM; |
| 522 | |
| 523 | chip->dev = &client->dev; |
| 524 | chip->regmap = devm_regmap_init_i2c(client, &i2c_pmic_regmap_config); |
| 525 | if (!chip->regmap) |
| 526 | return -ENODEV; |
| 527 | |
| 528 | i2c_set_clientdata(client, chip); |
| 529 | if (!of_property_read_bool(chip->dev->of_node, "interrupt-controller")) |
| 530 | goto probe_children; |
| 531 | |
| 532 | chip->domain = irq_domain_add_tree(client->dev.of_node, |
| 533 | &i2c_pmic_domain_ops, chip); |
| 534 | if (!chip->domain) { |
| 535 | rc = -ENOMEM; |
| 536 | goto cleanup; |
| 537 | } |
| 538 | |
| 539 | rc = i2c_pmic_parse_dt(chip); |
| 540 | if (rc < 0) { |
| 541 | pr_err("Couldn't parse device tree rc=%d\n", rc); |
| 542 | goto cleanup; |
| 543 | } |
| 544 | |
| 545 | rc = i2c_pmic_determine_initial_status(chip); |
| 546 | if (rc < 0) { |
| 547 | pr_err("Couldn't determine initial status rc=%d\n", rc); |
| 548 | goto cleanup; |
| 549 | } |
| 550 | |
| 551 | if (chip->pinctrl_name) { |
| 552 | chip->pinctrl = devm_pinctrl_get_select(chip->dev, |
| 553 | chip->pinctrl_name); |
| 554 | if (IS_ERR(chip->pinctrl)) { |
| 555 | pr_err("Couldn't select %s pinctrl rc=%ld\n", |
| 556 | chip->pinctrl_name, PTR_ERR(chip->pinctrl)); |
| 557 | rc = PTR_ERR(chip->pinctrl); |
| 558 | goto cleanup; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | rc = devm_request_threaded_irq(&client->dev, client->irq, NULL, |
| 563 | i2c_pmic_irq_handler, |
| 564 | IRQF_ONESHOT | IRQF_SHARED, |
| 565 | "i2c_pmic_stat_irq", chip); |
| 566 | if (rc < 0) { |
| 567 | pr_err("Couldn't request irq %d rc=%d\n", client->irq, rc); |
| 568 | goto cleanup; |
| 569 | } |
| 570 | |
| 571 | enable_irq_wake(client->irq); |
| 572 | |
| 573 | probe_children: |
| 574 | of_platform_populate(chip->dev->of_node, NULL, NULL, chip->dev); |
| 575 | pr_info("I2C PMIC probe successful\n"); |
| 576 | return rc; |
| 577 | |
| 578 | cleanup: |
| 579 | if (chip->domain) |
| 580 | irq_domain_remove(chip->domain); |
| 581 | i2c_set_clientdata(client, NULL); |
| 582 | return rc; |
| 583 | } |
| 584 | |
| 585 | static int i2c_pmic_remove(struct i2c_client *client) |
| 586 | { |
| 587 | struct i2c_pmic *chip = i2c_get_clientdata(client); |
| 588 | |
| 589 | of_platform_depopulate(chip->dev); |
| 590 | if (chip->domain) |
| 591 | irq_domain_remove(chip->domain); |
| 592 | i2c_set_clientdata(client, NULL); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | #ifdef CONFIG_PM_SLEEP |
| 597 | static int i2c_pmic_suspend(struct device *dev) |
| 598 | { |
| 599 | struct i2c_pmic *chip = dev_get_drvdata(dev); |
| 600 | struct i2c_pmic_periph *periph; |
| 601 | int rc = 0, i; |
| 602 | |
| 603 | for (i = 0; i < chip->num_periphs; i++) { |
| 604 | periph = &chip->periph[i]; |
| 605 | |
| 606 | rc = regmap_write(chip->regmap, |
| 607 | periph->addr | INT_EN_CLR_OFFSET, 0xFF); |
| 608 | if (rc < 0) { |
| 609 | pr_err_ratelimited("Couldn't clear 0x%04x irqs rc=%d\n", |
| 610 | periph->addr, rc); |
| 611 | continue; |
| 612 | } |
| 613 | |
| 614 | rc = regmap_write(chip->regmap, |
| 615 | periph->addr | INT_EN_SET_OFFSET, |
| 616 | periph->wake); |
| 617 | if (rc < 0) |
| 618 | pr_err_ratelimited("Couldn't enable 0x%04x wake irqs 0x%02x rc=%d\n", |
| 619 | periph->addr, periph->wake, rc); |
| 620 | } |
| 621 | |
| 622 | return rc; |
| 623 | } |
| 624 | |
| 625 | static int i2c_pmic_resume(struct device *dev) |
| 626 | { |
| 627 | struct i2c_pmic *chip = dev_get_drvdata(dev); |
| 628 | struct i2c_pmic_periph *periph; |
| 629 | int rc = 0, i; |
| 630 | |
| 631 | for (i = 0; i < chip->num_periphs; i++) { |
| 632 | periph = &chip->periph[i]; |
| 633 | |
| 634 | rc = regmap_write(chip->regmap, |
| 635 | periph->addr | INT_EN_CLR_OFFSET, 0xFF); |
| 636 | if (rc < 0) { |
| 637 | pr_err("Couldn't clear 0x%04x irqs rc=%d\n", |
| 638 | periph->addr, rc); |
| 639 | continue; |
| 640 | } |
| 641 | |
| 642 | rc = regmap_write(chip->regmap, |
| 643 | periph->addr | INT_EN_SET_OFFSET, |
| 644 | periph->synced[IRQ_EN_SET]); |
| 645 | if (rc < 0) |
| 646 | pr_err("Couldn't restore 0x%04x synced irqs 0x%02x rc=%d\n", |
| 647 | periph->addr, periph->synced[IRQ_EN_SET], rc); |
| 648 | } |
| 649 | |
| 650 | return rc; |
| 651 | } |
| 652 | #endif |
| 653 | static SIMPLE_DEV_PM_OPS(i2c_pmic_pm_ops, i2c_pmic_suspend, i2c_pmic_resume); |
| 654 | |
| 655 | static const struct of_device_id i2c_pmic_match_table[] = { |
| 656 | { .compatible = "qcom,i2c-pmic", }, |
| 657 | { }, |
| 658 | }; |
| 659 | |
| 660 | static const struct i2c_device_id i2c_pmic_id[] = { |
| 661 | { "i2c-pmic", 0 }, |
| 662 | { }, |
| 663 | }; |
| 664 | MODULE_DEVICE_TABLE(i2c, i2c_pmic_id); |
| 665 | |
| 666 | static struct i2c_driver i2c_pmic_driver = { |
| 667 | .driver = { |
| 668 | .name = "i2c_pmic", |
| 669 | .owner = THIS_MODULE, |
| 670 | .pm = &i2c_pmic_pm_ops, |
| 671 | .of_match_table = i2c_pmic_match_table, |
| 672 | }, |
| 673 | .probe = i2c_pmic_probe, |
| 674 | .remove = i2c_pmic_remove, |
| 675 | .id_table = i2c_pmic_id, |
| 676 | }; |
| 677 | |
| 678 | module_i2c_driver(i2c_pmic_driver); |
| 679 | |
| 680 | MODULE_LICENSE("GPL v2"); |
| 681 | MODULE_ALIAS("i2c:i2c_pmic"); |